package org.jeecg.modules.tankerRegistration.util;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.RestUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

@Component
public class TemplateMessageUtil {

    private static final Logger log = LoggerFactory.getLogger(TemplateMessageUtil.class);
    private final RedisUtil redisUtil;

    private static final String appid = "wxeba7a1e277a4e2aa";
    private static final String secret = "87f4e029ae803656c814d6ef02f0883a";

    private static final String[] transport = {"", "提货", "送货", "液袋"};

    public TemplateMessageUtil(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }

    /**
     * 推送司机三载审核结果
     * @param touser
     * @param plateNo
     * @param transportStatus
     * @param reason
     * @param date
     */
    public void register(String touser, String plateNo, Integer transportStatus, String reason, Date date) {
        String path="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+getToken();
        JSONObject jsonObj=new JSONObject();

        JSONObject data=new JSONObject();

        JSONObject car_number3=new JSONObject();
        car_number3.put("value",plateNo);

        JSONObject short_thing7=new JSONObject();
        short_thing7.put("value",transport[transportStatus]);

        JSONObject thing11=new JSONObject();
        thing11.put("value", reason);

        JSONObject time1=new JSONObject();
        time1.put("value", DateUtils.formatTime(date));

        data.put("car_number3",car_number3);
        data.put("short_thing7",short_thing7);
        data.put("thing11",thing11);
        data.put("time1",time1);

        jsonObj.put("touser",touser);//接收者openid
        jsonObj.put("template_id", "_Wg9UmPT1EY7g0Ng1bvIiDop1BUMiorFBnpRdMHWMcs");//模板ID
        jsonObj.put("url", "http://zclygcdj.ydyjk.com.cn?tanker=2");//模板跳转链接
        jsonObj.put("data",data);//模板数据

        log.info("推送司机三载审核结果接收者openid: {}", touser);
        JSONObject result= RestUtil.post(path,jsonObj);
        log.info("推送司机三载审核结果响应: {}", result);
    }

    /**
     * 推送员工当前流程
     * @param touser
     * @param plateNo
     * @param transportStatus
     * @param reason
     */
    public void sendOA(String touser, String plateNo, Integer transportStatus, String reason) {
        String path="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+getToken();
        String template_id = "_Wg9UmPT1EY7g0Ng1bvIiDop1BUMiorFBnpRdMHWMcs";
        String url = "http://zclygcdj.ydyjk.com.cn?tanker=2";
        JSONObject jsonObj=new JSONObject();

        JSONObject data=new JSONObject();

        JSONObject car_number3=new JSONObject();
        car_number3.put("value",plateNo);

        JSONObject short_thing7=new JSONObject();
        short_thing7.put("value",transport[transportStatus]);

        JSONObject thing11=new JSONObject();
        thing11.put("value", reason);

        JSONObject time1=new JSONObject();
        time1.put("value", DateUtils.formatDateTime());

        data.put("car_number3",car_number3);
        data.put("short_thing7",short_thing7);
        data.put("thing11",thing11);
        data.put("time1",time1);

        jsonObj.put("touser",touser);//接收者openid
        jsonObj.put("template_id",template_id);//模板ID
        jsonObj.put("url", url);//模板跳转链接
        jsonObj.put("data",data);//模板数据

        log.info("推送员工当前流程接收者openid: {}", touser);
        JSONObject result= RestUtil.post(path,jsonObj);
        log.info("推送员工当前流程响应: {}", result);
    }

    /**
     * 提醒司机上传铅封
     * @param touser
     * @param plateNo
     * @param driverName
     */
    public void sendLeadSealing(String touser, String plateNo, String driverName, String message) {
        String path="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+getToken();
        JSONObject jsonObj=new JSONObject();

        JSONObject data=new JSONObject();

        JSONObject car_number6=new JSONObject();
        car_number6.put("value",plateNo);

        JSONObject thing7=new JSONObject();
        thing7.put("value", driverName);

        JSONObject phrase3=new JSONObject();
        phrase3.put("value", message);

        JSONObject time4=new JSONObject();
        time4.put("value", DateUtils.formatDateTime());

        data.put("car_number6",car_number6);
        data.put("thing7",thing7);
        data.put("phrase3",phrase3);
        data.put("time4",time4);

        jsonObj.put("touser",touser);//接收者openid
        jsonObj.put("template_id", "8uvwAMfP-l4HsCgaKt5VAHICDBOIBMhiR2s5TC74bnk");//模板ID
        jsonObj.put("url", "http://zclygcdj.ydyjk.com.cn?tanker=3");//模板跳转链接
        jsonObj.put("data",data);//模板数据

        log.info("提醒司机上传铅封接收者openid: {}", touser);
        JSONObject result= RestUtil.post(path,jsonObj);
        log.info("提醒司机上传铅封响应: {}", result);
    }

    public String getToken() {
        if(redisUtil.hasKey("zcl_wx_token")){
            Object token=redisUtil.get("zcl_wx_token");
            if(token!=null&& !StringUtils.isEmpty(token.toString())){
                return token.toString();
            }else{
                redisUtil.set("zcl_wx_token",getWXToken(),7000);
            }
        }else{
            redisUtil.set("zcl_wx_token",getWXToken(),7000);
        }
        return redisUtil.get("zcl_wx_token").toString();
    }

    public String getWXToken() {
        String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ appid + "&secret=" + secret;
        RestTemplate restTemplate = new RestTemplate();
        String forObject = restTemplate.getForObject(url, String.class);
        JSONObject jsonObj = JSONObject.parseObject(forObject);
        if(jsonObj.containsKey("access_token")){
            return jsonObj.getString("access_token");
        } else {
            log.info("获取token失败: {}", jsonObj);
        }
        return "";
    }
}
